import os
from os import listdir
from os.path import isfile, join
import urllib
import urllib2

import httplib
import mimetypes
import MultipartPostHandler


def init(ptr_current_oper):
    """
    Where everything begins
    """
    #ptr_current_oper = 'lat'
    print "-----------------------------------------------"
    print "      starting loading sequence   "
    print "-----------------------------------------------"
    
    # get all lats
    latpath =  os.path.abspath( ptr_current_oper )   
    
    onlyfiles = [ f for f in listdir(latpath) if isfile(join(latpath,f)) ]
    
    int_file_cntr = 0
    
    # prep the post
    
    str_host = 'http://rel01dev05.innova-t.appspot.com'
    str_selector = '/marovi/processDetailFile/'
    
    for latfile in onlyfiles:
        int_file_cntr += 1
        
        if latfile.find("txt") == -1:
            continue
        
        print "file: %d %s"  % (int_file_cntr, latfile)
        # process each file
        # we send each file
        
        filepath = ptr_current_oper +'/' + latfile
        
        params = { 'sourceFile' : file(filepath, 'rb'), 
                  'resolution':'0.25', 
                  'year':'2012',
                  'day':'273',
                  'hour':'3',
                  'loadNumber':'23',
                    }
        opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
        str_resp = opener.open(str_host + str_selector, params).read()
        print str_resp

#    print "now we go for the longitude"
#    if ptr_current_oper == 'lng':
#        return
#    else:
#        init('lng')
    
    print "the end"
            

if __name__ == '__main__':
    init('salida')